package com.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.base.service.DepartmentService;
import com.common.bean.Department;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;

public class DepartmentServiceImpl extends AbstractManager implements
		DepartmentService {
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	
	public void addDepartment(Object obj) {
		this.getHibernateTemplate().save(obj);
	}
	
	//需要创建deptcode、tid、pid	
	public void addDepartment(Object obj, String supdept) {
		Department supdepartment = (Department)findDepartment(supdept);
		Department department = (Department)obj;
		
		Long pid = 0l;
		String deptcode = "";
		if(supdepartment != null && supdepartment.getTid() != 0) {//有上级部门
			List list = findAllChild(supdepartment.getTid());
			int n = 1 ;
			
			for(Object ol:list) {
				Department dept = (Department)ol;
				if(n < dept.getTid()) {
					n = dept.getTid().intValue() ;
				}
			}
			
			n += 1;
			
			deptcode = supdepartment.getDeptcode() + ( ( n < 10 ) ? ("0" + n ): n);
			pid = supdepartment.getTid();
		}
		else {//没有上级部门
			//查询所有pid为0的顶级节点的数量
			String hql = "from Department dept where dept.pid = 0 " ;
			List pl = this.getHibernateTemplate().find(hql);
			int n = 0;
			
			for(Object ol:pl) {
				Department dept = (Department)ol;
				if(n < dept.getTid()) {
					n = dept.getTid().intValue() + 1;
				}
			}
			
			deptcode = "" + ( ( n < 10 ) ? ("0" + n ): n);
			pid = 0l;
		}
		
		Long tid = findAllDepartmentSize(); //创建树节点id
		String target = "content";
		String url = "updateDept.action?deptcode="+deptcode;
		
		department.setDeptcode(deptcode);
		department.setTid(tid);
		department.setPid(pid);
		department.setTarget(target);
		department.setUrl(url);
		
		this.getHibernateTemplate().save(department);
	}
	
	public List findAllDepartment() {
		String hql = "from Department ";
		return this.getHibernateTemplate().find(hql);
	}
	
	public PagerModel findAllDept(String deptcode) {
		Department dept = (Department)findDepartment(deptcode);
		String hql = "";
		Long pid = 0l;
		
		if(dept != null) {
			pid = dept.getTid();
		}
		
		hql = "from Department dept where dept.pid = " + pid;
		
		return searchPaginated(hql);
	}

	public Object findDepartment(String deptcode) {
		if(deptcode == null || deptcode == "" ) return null;
		return this.getHibernateTemplate().get(Department.class, deptcode);
	}
	
	//不处理级联删除的情况
	@SuppressWarnings("unchecked")
	public void removeDepartment(String deptcode) {
		Department department = (Department)findDepartment(deptcode);
		if(department == null) {
			logger.info(" 操作非法！");
			throw new RuntimeException("该部门不存在！");
		}
		List<Department> list = findAllChild(department.getTid());
		if(list != null && list.size() > 0) {
			for(Department dept:list) {
				removeDepartment(dept.getDeptcode());//迭代删除！其他表中已用数据需慎重
			}
		}
		
		this.getHibernateTemplate().delete(department);
	}

	public void updateDepartment(Object obj) {
		Department dept = (Department)obj;
		
		String deptcode = dept.getDeptcode();
		Department department = (Department)findDepartment(deptcode);
		
		if(department == null) {
			logger.info(" 操作非法！");
			throw new RuntimeException("该部门不存在！");
		}
		
		department.setDeptadress(dept.getDeptadress());
		department.setDeptcaption(dept.getDeptcaption());
		department.setDeptlinkname(dept.getDeptlinkname());
		department.setDeptphone(dept.getDeptphone());
		
		this.getHibernateTemplate().saveOrUpdate(department);
	}

	public List findAllChild(Long tid) {
		String hql = "from Department dept where dept.pid = " + tid;
		return this.getHibernateTemplate().find(hql);
	}
	
	public Long findAllDepartmentSize() {
		String hql = "select nvl(max(dept.tid),0) from Department dept";
		return (Long)this.getHibernateTemplate().find(hql).get(0) + 1l;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map> findAllDeptMap() {
		List<Department> deptlist = findAllDepartment();
		
		List<Map> coll = new ArrayList<Map>();
		Map<String,Object> info ;
		
		for(Department dept:deptlist) {
			info = new HashMap<String,Object>();
			info.put("id", dept.getTid());
			info.put("pid",dept.getPid());
			info.put("name",dept.getDeptcaption());
			info.put("url",dept.getUrl());
			info.put("target", dept.getTarget());
			coll.add(info);
		}
		
		return coll;
	}

	public List findAllleafDepartment() {
		String hql = "from Department s where s.tid not in " +
				"(select distinct t.pid from Department t) order by s.tid" ;
		return this.getHibernateTemplate().find(hql);
	}

	public PagerModel findDeptByDeptcaption(String deptcaption) {
		String hql = "from Department dept where dept.deptcaption like '%" + deptcaption + "%' order by dept.deptcode";
		return searchPaginated(hql);
	}
}
